import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'action.dart';
import 'state.dart';
import 'dart:io';

Widget buildView(
    WebViewComponentState state, Dispatch dispatch, ViewService viewService) {
  if (state.requestLoad && state.webViewController != null) {
    dispatch(WebViewComponentActionCreator.onRequestUrl());
  } else if (state.reRresh) {
    dispatch(WebViewComponentActionCreator.onRefresh());
  }
  return SafeArea(
      child: SizedBox.expand(
        child: WebView(
          onWebViewCreated: (WebViewController controller) =>
              dispatch(WebViewComponentActionCreator.onWebViewCreate(controller)),
          javascriptMode: JavascriptMode.unrestricted,
          onPageFinished: (url) =>
              dispatch(WebViewComponentActionCreator.onPageFinish()),
          navigationDelegate: (NavigationRequest request) {
//            if (request.url.contains('category/id'))
//              return NavigationDecision.navigate;
//            if (Platform.isIOS) {
//              if (request.url.contains('store/cart') && state.isShopCar)
//                return NavigationDecision.navigate;
//              if (request.url.contains('mobile/find/index') && state.isShopCar)
//                return NavigationDecision.navigate;
//              if (request.url.contains('address/setdefault/'))
//                return NavigationDecision.navigate;
//            }
//            dispatch(WebViewComponentActionCreator.onHandleUrl(request.url));
//            return NavigationDecision.prevent;
            print('webview:${request.url}');
            if (request.url.startsWith('alipays://')||request.url.startsWith('alipay://')) {
              dispatch(WebViewComponentActionCreator.onHandleUrl(request.url));
              return NavigationDecision.prevent;
            }
            return NavigationDecision.navigate;
          },
        ),
      ));
}
